import axios from '@/libs/api.request'

export default {
  storeName: 'userStore',
  namespaced: true,
  state: {

  },
  mutations: {

  },
  actions: {
    getValidateRule () {
      return {
        account: [
          { required: true, message: '登录账号不能为空', trigger: 'blur' }
        ],
        userPass: [
          { required: true, message: '登录密码不能为空', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
        ]
      }
    },
    loadList ({ state, commit }, { pageIndex, pageSize }) {
      return axios.request({
        url: '/api/OauthUser/list',
        params: {
          pageIndex,
          pageSize
        },
        method: 'get'
      })
    },
    getData ({ state, commit }, { userId }) {
      return axios.request({
        url: '/api/OauthUser/find',
        params: {
          id: userId
        },
        method: 'get'
      })
    },
    save ({ state, commit }, vo) {
      const requestUrl = (vo.userId && vo.userId !== '') ? '/api/OauthUser/update' : '/api/OauthUser/add'
      return axios.request({
        url: requestUrl,
        method: 'post',
        data: vo
      })
    },
    delete ({ state, commit }, { userId }) {
      return axios.request({
        url: 'api/OauthUser/delete',
        method: 'post',
        data: {id: userId},
        transformRequest: [function (data) {
          let ret = ''
          for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
          }
          return ret
        }],
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      })
    }
  }
}
